package com.itheima.controller;


import com.github.pagehelper.Page;

import com.itheima.pojo.*;

import com.itheima.pojo.Result;
import com.itheima.pojo.TagVO;
import com.itheima.pojo.StatusCode;
import com.itheima.pojo.Tag;

import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/tag")
public class TagController {


    @Autowired
    private TagService tagService;

    //1 增加标签 /add
    @RequestMapping("/add")
    public Result add(@RequestBody TagVO tagVO) {
        Tag tag;
        try {
            tag = new Tag();
            tag.setContent(tagVO.getContent());
            tag.setSort(tagVO.getSort());
            tagService.add(tag);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "插入失败");
        }
        //{"code":"success","data":"插入成功"}
        return new Result(StatusCode.OK, "插入成功");
    }

    //2 批量删除标签  /tag/deleteBatch
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<TagVO> list) {
        Result result;
        try {
            result=tagService.deleteBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
           return  null;
        }
        return result;
    }


    //3 编辑标签   /tag/edit
    @RequestMapping("/edit")
    public Result edit(@RequestBody TagVO tagVO) {
        Tag tag;
        try {
            tag = new Tag();
            tag.setContent(tagVO.getContent());
            tag.setSort(tagVO.getSort());
            tag.setUid(tagVO.getUid());
            tag.setStatus(tagVO.getStatus());
            tag.setClickCount(tagVO.getClickCount());
            tag.setUpdateTime(new Date());
            tag.setCreateTime(new Date());
            tagService.edit(tag);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "更新失败");
        }
        return new Result(StatusCode.OK, "更新成功");
    }


    //4 获取标签列表  POST /tag/getList
    @RequestMapping("/getList")
    public Map getList(@RequestBody TagVO tagVO) throws Exception {
        Page<Tag> page = tagService.getList(tagVO);

        List<Tag> tagList = page.getResult();
        ArrayList<Map<String,Object>> list1 = new ArrayList<>();

        for (Tag tag : tagList) {
            Map<String, Object> map2 = new HashMap<>();
            map2.put("content",tag.getContent());
            map2.put("clickCount",tag.getClickCount());
            map2.put("sort",tag.getSort());
            map2.put("uid",tag.getUid());
            map2.put("status",tag.getStatus());
            map2.put("createTime", DateUtils.parseDate2String(tag.getCreateTime()));
            map2.put("updateTime", DateUtils.parseDate2String(tag.getUpdateTime()));
            list1.add(map2);
        }

        long total = page.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", list1);
        map.put("total", total);
        map.put("size", tagVO.getPageSize());
        map.put("current", tagVO.getCurrentPage());
        ArrayList<Object> list = new ArrayList<>();
        map.put("orders", list);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);
        Map<String, Object> result= new HashMap<>();
        result.put("code", "success");
        result.put("data", map);
        return result;
    }


    //5  置顶标签    /tag/stick   //根据sort 值越大越靠前
    @RequestMapping("/stick")
    public Result stick(@RequestBody TagVO tagVO) {
         //{"code":"success","data":"操作成功"}
        // {"code":"error","data":"该标签已经在顶端！"}
        try {
            tagService.stick(tagVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "该标签已经在顶端");
        }
        return new Result(StatusCode.OK, "操作成功");
    }


    //6 通过引用量排序标签  POST /tag/tagSortByCite
    @RequestMapping("/tagSortByCite")
    public Result tagSortByCite() {
        try {
            tagService.tagSortByCite();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "操作失败");
        }
        return new Result(StatusCode.OK, "操作成功");
    }


    //7 通过点击量排序标签  /tag/tagSortByClickCount
    @RequestMapping("/tagSortByClickCount")
    public Result tagSortByClickCount() {
        try {
            tagService.tagSortByClickCount();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "操作失败");
        }
        return new Result(StatusCode.OK, "操作成功");
    }


}
